<template>
  <div style="width: 80%">
    <div style="margin-bottom: 30px">新增租借订单</div>
    <el-form :inline="true" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <!-- pro与rules中的属性对应 -->
<!--      <el-form-item label="租借单号" prop="hireNo">-->
<!--        <el-input v-model="form.hireNo" placeholder="请输入租借单号"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="用户名" prop="userName">
        <el-select v-model="form.userName" @change="selUser" filterable placeholder="请选择用户名">
          <el-option
              v-for="item in users"
              :key="item.name"
              :label="item.userName"
              :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="form.userId" :disabled="true"></el-input>
      </el-form-item>

      <br/>

      <el-form-item label="单车名称" prop="bikeName">
        <el-select v-model="form.bikeName" @change="selBike" filterable placeholder="请选择单车">
          <el-option
              v-for="item in bikes"
              :key="item.name"
              :label="item.bikeName"
              :value="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单车ID" prop="bikeId">
        <el-input v-model="form.bikeId" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="单车种类" prop="category">
        <el-input v-model="form.category" :disabled="true"></el-input>
      </el-form-item>






<!--      <el-form-item label="分类" prop="categories">-->
<!--        <el-cascader v-model="form.categories" :options="categories" :props="{value: 'name', label: 'name'}">-->

<!--        </el-cascader>-->
<!--      </el-form-item>-->
    </el-form>

    <div style="text-align: center; margin-top: 30px">
      <el-button type="primary" @click="save" size="medium">提交</el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: 'AddBike',
  data() {
    return {
      form: {},
      users: [],
      bikes: [],
      rules: {
        userName: [
          {required: true, message: '请选择用户', trigger: 'blur'},
        ],
        bikeName: [
          {required: true, message: '请选择单车', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    request.get('/hire/').then(res => {
      this.hires = res.data;
    })
    request.get('/user/').then(res => {
      this.users = res.data;
    })
    request.get('/bike/').then(res => {
      this.bikes = res.data;
    })
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request.post('/hire/', this.form).then(res => {
            if (res.code === '200') {
              this.$notify.success('新增成功');
              this.$refs['ruleForm'].resetFields();
            } else {
              this.$notify.error(res.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    selUser(){
      //user里的属性是java中实体类对应的属性
      const user = this.users.find(v => v.name === this.form.userName);
      this.form.userId = user.id;
    },
    selBike(){
      const bike = this.bikes.find(v => v.name === this.form.bikeName);
      this.form.bikeId = bike.id;
      this.form.category = bike.category;

    }
  }

}

</script>

